package com.qg.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import com.qg.pojo.QgGoods;
import com.qg.pojo.QgGoodsTempStock;
import com.qg.pojo.QgUser;
import com.qg.service.LocailGoodsService;
import com.qg.service.QgGoodsService;
import com.qg.service.QgGoodsTempStockService;
import com.qg.utils.Constants;
import com.qg.utils.EmptyUtils;
import com.qg.utils.RedisUtil;
import com.zxs.resp.ReturnResult;
import com.zxs.util.ReturnResultUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import vo.GoodsVo;
import vo.QgGoodsMessageVo;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;




public class LocailGoodsServiceImpl implements LocailGoodsService {
    @Reference
    private QgGoodsService qgGoodsService;
    private QgGoodsTempStockService qgGoodsTempStockService;

    @Autowired
    private RedisUtil redisUtil;

    /**
     * 获取商品信息
     * @param id
     * @return
     * @throws Exception
     */
    @Override
    public ReturnResult queryGoodsById(String id) throws Exception {
        GoodsVo goodsVo=null;
     String goodsVoJSON= redisUtil.getStr(Constants.goodsprefix+id);
        if (EmptyUtils.isEmpty(goodsVoJSON)) {
           goodsVo=new GoodsVo();
            //获取商品信息
            QgGoods qgGoods=qgGoodsService.getQgGoodsById(id);
            //将qgGoods同名属性拷到goodsVo
            BeanUtils.copyProperties(qgGoods,goodsVo);
            //获取库存信息
            //获取临时库存表中goodid
            Map<String,Object> param=new HashMap<>();
            param.put("goodsId",id);
            param.put("active",1);
            Integer count= qgGoodsTempStockService.getQgGoodsTempStockCountByMap(param);
            //实际库存=总库存-剩余库存
            Integer currentCount=goodsVo.getStock()-count;
            goodsVo.setCurrentStock(currentCount);
            //放再redis当中
            redisUtil.setStr(Constants.goodsprefix+id, JSONObject.toJSONString(goodsVo));
        }else {
            goodsVo=JSONObject.parseObject(goodsVoJSON,GoodsVo.class);
        }
        return ReturnResultUtil.returnSuccess(String.valueOf(goodsVo));
    }

    @Override
    public ReturnResult getGoods(String taken, String goodsId) throws Exception {
        //1.根据taken获取用户信息
        String userJSON=redisUtil.getStr(taken);
        QgUser qgUser= JSONObject.parseObject(userJSON,QgUser.class);
        //2.查看用户是否抢购该商品
        Map<String,Object> param=new HashMap<String, Object>();
        param.put("goodsId",goodsId);
        param.put("active",1);
        param.put("userId",qgUser.getId());
        Integer activecount=qgGoodsTempStockService.getQgGoodsTempStockCountByMap(param);
        if(activecount>0){
            return ReturnResultUtil.returnFail("你以抢购过该商品");
        }
        String goodsJSON=redisUtil.getStr(Constants.goodsprefix+goodsId);
        GoodsVo qgGoodsVo=JSONObject.parseObject(goodsJSON,GoodsVo.class);
        //判断库存是否充足
        if (qgGoodsVo.getCurrentStock()<=0){
            return ReturnResultUtil.returnFail("商品库存不足");
        }
        //更新库存表数据
        QgGoodsTempStock qgGoodsTempStock1=new QgGoodsTempStock();
        //qgGoodsTempStock1.setId(IdWorker.getId());
        qgGoodsTempStock1.setGoodsId(goodsId);
        qgGoodsTempStock1.setUserId(qgUser.getId());
        qgGoodsTempStock1.setStatus(1);
        qgGoodsTempStock1.setCreatedTime(new Date());
        qgGoodsTempStock1.setUpdatedTime(null);
        qgGoodsTempStockService.qdtxAddQgGoodsTempStock(qgGoodsTempStock1);
        qgGoodsVo.setCurrentStock(qgGoodsVo.getCurrentStock()-1);
        String newJSON=JSONObject.toJSONString(qgGoodsVo);
        redisUtil.setStr(Constants.goodsprefix+qgGoodsVo.getId(),newJSON);
        return ReturnResultUtil.returnSuccess("抢购成功");
    }

    @Override
    public ReturnResult sendGoodsMessage(String token, String goodsId) {
        String userJson =  redisUtil.getStr(token);
        JSONObject.parseObject(userJson,QgUser.class);
        String userId;
        if (EmptyUtils.isEmpty(qgUser)){
            userId = qgUser.getId();
            QgGoodsMessageVo qgGoodsMessage = new QgGoodsMessageVo();
            qgGoodsMessage.setUserId(userId);
            qgGoodsMessage.setGoodsId(goodsId);
            activeMQUtils.sendMessage("qg:goods","im message");

            return ReturnResultUtil.returnSuccess();
        }

        return ReturnResultUtil.returnFail("出错");

}
